package com.douyuehan.doubao.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class ExcelUtil {
    /**
     * 导出数据生成EXCEL方法
     * @param request
     * @param response
     * @param list
     * @throws IOException
     */
    //public static void exportExcelBook(HttpServletRequest request, HttpServletResponse response, List<OrdersCompleted> list) throws IOException {
    public static void exportExcelBook(HttpServletRequest request, HttpServletResponse response, List list) throws IOException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        //文件名称，客户端传来的参数，防止中文文件名乱码参数编码因此这里需要解码
        //String fileName = URLDecoder.decode(request.getParameter("fileName"),"UTF-8");
        String fileName = "parking_orders_completed";
        //创建Excel工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet();
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 2500);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 6000);
        sheet.setColumnWidth(6, 6000);

        // 设置表头字体样式
        HSSFFont columnHeadFont = workbook.createFont();
        columnHeadFont.setFontName("宋体");
        columnHeadFont.setFontHeightInPoints((short) 10);
        columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        // 列头的样式
        HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
        columnHeadStyle.setFont(columnHeadFont);
        // 左右居中
        columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 上下居中
        columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        columnHeadStyle.setLocked(true);
        columnHeadStyle.setWrapText(true);
        // 左边框的颜色
        columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
        // 边框的大小
        columnHeadStyle.setBorderLeft((short) 1);
        // 右边框的颜色
        columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
        // 边框的大小
        columnHeadStyle.setBorderRight((short) 1);
        // 设置单元格的边框为粗体
        columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        // 设置单元格的边框颜色
        columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
        // 设置单元格的背景颜色（单元格的样式会覆盖列或行的样式）
        columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
        // 设置普通单元格字体样式
        HSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);

        //创建Excel工作表第一行
        HSSFRow row0 = sheet.createRow(0);
        // 设置行高
        row0.setHeight((short) 750);
        HSSFCell cell = row0.createCell(0);
        //设置单元格内容
        cell.setCellValue(new HSSFRichTextString("id"));
        //设置单元格字体样式
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(1);
        cell.setCellValue(new HSSFRichTextString("project_id"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(2);
        cell.setCellValue(new HSSFRichTextString("site_no"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(3);
        cell.setCellValue(new HSSFRichTextString("customer_name"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(4);
        cell.setCellValue(new HSSFRichTextString("customer_phone"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(5);
        cell.setCellValue(new HSSFRichTextString("customer_code"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(6);
        cell.setCellValue(new HSSFRichTextString("customer_tag"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(7);
        cell.setCellValue(new HSSFRichTextString("add_time"));
        cell.setCellStyle(columnHeadStyle);

        // 循环写入数据
//        for (int i = 0; i < list.size(); i++) {
//            OrdersCompleted stu = list.get(i);
//            HSSFRow row = sheet.createRow(i + 1);
//            cell = row.createCell(0);
//            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getId())));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(1);
//            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getProjectId())));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(2);
//            cell.setCellValue(new HSSFRichTextString(stu.getSiteNo()));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(3);
//            cell.setCellValue(new HSSFRichTextString(stu.getCustomerName()));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(4);
//            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getCustomerPhone())));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(5);
//            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getCustomerCode())));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(6);
//            cell.setCellValue(new HSSFRichTextString(stu.getCustomerTag()));
//            cell.setCellStyle(columnHeadStyle);
//            cell = row.createCell(7);
//            cell.setCellValue(new HSSFRichTextString());
//            cell.setCellStyle(columnHeadStyle);
//            cell.setCellValue(new HSSFRichTextString(DateUtil.dateToStr(stu.getAddTime(), "yyyy-MM-dd HH:mm:ss")));
//        }

        // 获取输出流
        OutputStream os = response.getOutputStream();
        // 重置输出流
        response.reset();
        // 设定输出文件头
        response.setHeader("Content-disposition",
                "attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
        // 定义输出类型
        response.setContentType("application/msexcel");
        workbook.write(os);
        os.close();
        return;
    }
}
